﻿using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.LodingForm;
using Sunny.UI;
using System;
using System.Management;
using System.Text;

namespace DevelopTool.Forms.SystemToolForms
{
    /// <summary>
    /// 电脑环境
    /// </summary>
    public partial class SystemEnvironmentForm : BaseUIPage
    {
        public SystemEnvironmentForm()
        {
            InitializeComponent();
        }

        private void SystemEnvironmentForm_Load(object sender, EventArgs e)
        {
            //防止界面卡死，所以延迟1秒后调用方法~
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer
            {
                Interval = 500,
                Enabled = true//启动计时器
            };
            timer.Tick += delegate (object ss, EventArgs ee)
            {
                timer.Stop();
                ShowSystemInfo();
            };

        }


        private void btnRelode_Click(object sender, EventArgs e)
        {
            ShowSystemInfo();
        }


        private void ShowSystemInfo()
        {
            StringBuilder sb = new StringBuilder();
            LoadingHelper.ShowLoading("获取计算机环境信息", () =>
            {
                sb.AppendLine("=========================================系统=========================================");
                ManagementClass info = new ManagementClass("Win32_OperatingSystem");//系统信息
                foreach (ManagementObject o in info.GetInstances())
                {
                    sb.AppendLine("系统名称：" + o["Caption"]);
                    foreach (ManagementObject o2 in info.GetInstances())
                    {
                        sb.AppendLine("计算机名称：" + o2["CSName"]);
                    }
                    sb.AppendLine("系统版本：" + o["Version"]);
                    sb.AppendLine("系统位宽：" + o["OSArchitecture"]);
                    sb.AppendLine("注册用户：" + o["RegisteredUser"]);
                    sb.AppendLine("安装日期：" + o["InstallDate"]);
                    sb.AppendLine("序列号：" + o["SerialNumber"]);
                }

                sb.AppendLine("\n=========================================.Net环境=========================================");
                sb.AppendLine(AssemblyHelper.Get45PlusFromRegistry());
                sb.AppendLine(AssemblyHelper.GetVersionFromRegistry());

                sb.AppendLine("\n=========================================JAVA环境=========================================");
                sb.AppendLine(SystemEx.RunDosCmd("javac -version"));

                sb.AppendLine("\n=========================================CPU=========================================");
                info = new ManagementClass("Win32_Processor");//处理器信息
                foreach (ManagementObject o in info.GetInstances())
                {
                    sb.AppendLine("CPU名称：" + o["name"]);
                    sb.AppendLine("CPU编号：" + o["ProcessorId"]);
                    sb.AppendLine("CPU版本：" + o["Version"]);
                    sb.AppendLine("CPU位宽：" + o["AddressWidth"]);
                    sb.AppendLine("CPU主频：" + o["CurrentClockSpeed"]);
                    sb.AppendLine("CPU外频：" + o["ExtClock"]);
                    sb.AppendLine("CPU接口：" + o["SocketDesignation"]);
                    sb.AppendLine("CPU制造商：" + o["Manufacturer"]);
                    sb.AppendLine("CPU核心数：" + o["NumberOfCores"]);
                    sb.AppendLine("CPU线程数：" + o["NumberOfLogicalProcessors"]);
                    sb.AppendLine("CPU L2缓存：" + o["L2CacheSize"]);
                    sb.AppendLine("CPU L3缓存：" + o["L3CacheSize"]);
                    sb.AppendLine("CPU电压：" + o["CurrentVoltage"]);
                    sb.AppendLine("虚拟化支持：" + o["VirtualizationFirmwareEnabled"]);
                    sb.AppendLine("Hyper-V支持：" + o["VMMonitorModeExtensions"]);
                }

                sb.AppendLine("\n=========================================主板=========================================");
                info = new ManagementClass("Win32_BaseBoard");//主板信息
                foreach (ManagementObject o in info.GetInstances())
                {
                    sb.AppendLine("主板型号：" + o["Product"]);
                    sb.AppendLine("主板制造商：" + o["Manufacturer"]);
                    sb.AppendLine("主机板：" + o["HostingBoard"]);
                    sb.AppendLine("热拔插支持：" + o["HotSwappable"]);
                }

                sb.AppendLine("\n=========================================内存=========================================");
                ManagementClass c = new ManagementClass("Win32_PhysicalMemory");//内存信息
                foreach (ManagementObject o in c.GetInstances())
                {
                    sb.AppendLine("内存编号：" + o["DeviceLocator"]);
                    sb.AppendLine("内存类型：" + o["Description"]);
                    sb.AppendLine("内存位宽：" + o["DataWidth"]);
                    sb.AppendLine("内存标签：" + o["BankLabel"]);
                    sb.AppendLine("内存容量：" + SystemEx.ToGB(Convert.ToInt64(o["Capacity"])) + "GB");
                    sb.AppendLine("内存频率：" + o["Speed"]);
                    sb.AppendLine("生产厂商：" + o["Manufacturer"]);
                    sb.AppendLine("热拔插支持：" + o["HotSwappable"]);
                    sb.AppendLine(Environment.NewLine);
                }

                sb.AppendLine("\n=========================================网卡=========================================");
                info = new ManagementClass("Win32_NetworkAdapter");//网卡信息
                foreach (ManagementObject o in info.GetInstances())
                {
                    if (o["Manufacturer"] != null && !o["Manufacturer"].ToString().Contains("Microsoft"))
                    {
                        sb.AppendLine("网卡编号：" + o["DeviceID"]);
                        sb.AppendLine("网卡名称：" + o["Name"]);
                        sb.AppendLine("网卡类型：" + o["AdapterType"]);
                        sb.AppendLine("物理设备：" + o["PhysicalAdapter"]);
                        sb.AppendLine("电源管理：" + o["PowerManagementSupported"]);
                        sb.AppendLine("产品名称：" + o["ProductName"]);
                        sb.AppendLine("网卡带宽：" + SystemEx.ToMB(Convert.ToInt64(o["Speed"])) + "MB");
                        sb.AppendLine("生产厂商：" + o["Manufacturer"]);
                        sb.AppendLine("设备描述：" + o["Description"]);
                        sb.AppendLine("MAC地址：" + o["MACAddress"]);
                        sb.AppendLine(Environment.NewLine);
                    }
                }

                sb.AppendLine("\n=========================================磁盘=========================================");
                info = new ManagementClass("Win32_DiskDrive");//磁盘信息
                foreach (ManagementObject o in info.GetInstances())
                {
                    sb.AppendLine("磁盘名称：" + o["Name"]);
                    sb.AppendLine("磁盘型号：" + o["Model"]);
                    sb.AppendLine("磁盘状态：" + o["Status"]);
                    sb.AppendLine("生产厂商：" + o["Manufacturer"]);
                    sb.AppendLine("磁盘大小：" + SystemEx.ToGB(Convert.ToInt64(o["Size"])) + "GB");
                    sb.AppendLine("磁盘描述：" + o["Description"]);
                    sb.AppendLine("柱面总数：" + o["TotalCylinders"]);
                    sb.AppendLine("磁头总数：" + o["TotalHeads"]);
                    sb.AppendLine("扇区总数：" + o["TotalSectors"]);
                    sb.AppendLine("序列号：" + o["SerialNumber"].ToString().Trim());
                    sb.AppendLine(Environment.NewLine);
                }

                sb.AppendLine("\n=========================================逻辑磁盘=========================================");
                info = new ManagementClass("Win32_LogicalDisk");//逻辑磁盘信息
                foreach (ManagementObject o in info.GetInstances())
                {
                    sb.AppendLine("设备ID：" + o["DeviceID"]);
                    sb.AppendLine("文件系统：" + o["FileSystem"]);
                    sb.AppendLine("卷序名称：" + o["VolumeName"]);
                    sb.AppendLine("卷序列号：" + o["VolumeSerialNumber"]);
                    sb.AppendLine("可用空间：" + SystemEx.ToGB(Convert.ToInt64(o["FreeSpace"])) + "GB");
                    sb.AppendLine("总共大小：" + SystemEx.ToGB(Convert.ToInt64(o["Size"])) + "GB");
                    sb.AppendLine("支持磁盘配额：" + o["SupportsDiskQuotas"]);
                    sb.AppendLine("支持文件压缩：" + o["SupportsFileBasedCompression"]);
                    sb.AppendLine(Environment.NewLine);
                }


                sb.AppendLine("\n=========================================BIOS=========================================");
                info = new ManagementClass("Win32_BIOS");//BIOS信息
                foreach (ManagementObject o in info.GetInstances())
                {
                    sb.AppendLine("BIOS名称：" + o["Name"]);
                    sb.AppendLine("生产厂商：" + o["Manufacturer"]);
                    sb.AppendLine("主要BIOS：" + o["PrimaryBIOS"]);
                    sb.AppendLine("BIOS版本：" + o["Version"]);
                }

                sb.AppendLine("\n=========================================监视器=========================================");
                info = new ManagementClass("Win32_DesktopMonitor");//监视器信息
                foreach (ManagementObject o in info.GetInstances())
                {
                    sb.AppendLine("监视器名称：" + o["Name"]);
                    sb.AppendLine("监视器类型：" + o["MonitorType"]);
                    sb.AppendLine("生产厂商：" + o["MonitorManufacturer"]);
                    sb.AppendLine("监视器编号：" + o["DeviceID"] + " <-");
                    sb.AppendLine("监视器高度：" + o["ScreenHeight"]);
                    sb.AppendLine("监视器宽度：" + o["ScreenWidth"]);
                    sb.AppendLine("电源管理支持：" + o["PowerManagementSupported"]);
                    sb.AppendLine(Environment.NewLine);
                }

                sb.AppendLine("\n=========================================显卡=========================================");
                ManagementClass v1 = new ManagementClass("Win32_DisplayConfiguration");//显卡信息 1
                ManagementClass v2 = new ManagementClass("Win32_VideoController");//显卡信息 2
                foreach (ManagementObject o in v1.GetInstances())
                {
                    sb.AppendLine("设备名称：" + o["DeviceName"]);
                    foreach (ManagementObject o2 in v2.GetInstances())
                    {
                        sb.AppendLine("芯片厂商：" + o2["AdapterCompatibility"]);
                        sb.AppendLine("出厂日期：" + o2["DriverDate"]);
                        sb.AppendLine("内存大小：" + SystemEx.ToGB(Convert.ToInt64(o2["AdapterRAM"])) + "GB");
                        sb.AppendLine("驱动程序：" + o2["InstalledDisplayDrivers"]);
                        sb.AppendLine("驱动版本：" + o2["DriverVersion"]);
                        sb.AppendLine("DAC类型：" + o2["AdapterDACType"]);
                        sb.AppendLine("最大刷新率：" + o2["MaxRefreshRate"]);
                        sb.AppendLine("最小刷新率：" + o2["MinRefreshRate"]);
                        sb.AppendLine("当前显示模式：" + o2["VideoModeDescription"]);
                    }
                }

                sb.AppendLine("\n=========================================声卡=========================================");
                ManagementClass l = new ManagementClass("Win32_SoundDevice");//声卡信息
                foreach (ManagementObject o in info.GetInstances())
                {
                    sb.AppendLine("设备名称：" + o["Caption"]);
                    //sb.AppendLine("制造商：" + o["Manufacturer"]);
                    sb.AppendLine("支持电源管理：" + o["PowerManagementSupported"]);
                    sb.AppendLine("当前状态：" + o["Status"]);
                }

            });
            this.rTxt.Text = sb.ToString();

        }


    }
}
